#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <numeric>
#include <ctime>
using namespace std;

class Person{
public:
    Person(string name);
//    Person(const Person* pstr);
    ~Person();

//    Person(const Person& rhs);
//    operator= Person(const Person& rhs);
//    Person& operator=(const Person& rhs);

//    friend ostream Person(ostream& os); 
    friend ostream& operator<<(ostream& os, const Person& rhs);

    void add_score(int score);
    double cal_ave_score();
private:
    string _name;
    deque<int> _scores;
};

Person::Person(string name)
    :_name(name){
}

//Person::Person(const Person& rhs)
//    :_name(rhs._name)
//    ,_score(rhs._score){}
//
//Person& Person::operator=(const Person& rhs){
//    _name = rhs._name;
//    _score = rhs._score;
//    return *this;
//}

ostream& operator<<(ostream& os, const Person& rhs){
//    cout << "name: " << rhs._name << "\n";
//    cout << "score: " << rhs._score << endl;
    os << "name: " << rhs._name << "\n"
       << "score: ";
    for(int score: rhs._scores){
        os << score << " ";
    }

    os << "\n";

    return os;
}

void Person::add_score(int score){
    _scores.push_back(score);
}

double Person::cal_ave_score(){
    if(_scores.empty()) return 0.0;

    cout << "原始数据: " << *this;
    std::sort(_scores.begin(), _scores.end());
    _scores.pop_back();
    _scores.pop_front();

    cout << "去掉极值: " << *this;
    int double_score = accumulate(_scores.begin(), _scores.end(), 0.0)/_scores.size();
    
    cout << "平均分: " << double_score << endl;
    return double_score;
}

Person::~Person(){}

void test(){
    std::srand(std::time(nullptr));
    Person A("Amy");
    Person B("Bob");
    Person C("Cindy");
    Person D("Devid");
    Person E("Emma");
    vector<Person> players = {A, B, C, D, E};  // 初始化5名选手

    // 模拟10个评委打分（随机60~100分）
    for (auto& player : players) {
        std::deque<int> tempScores;  // 临时存储当前选手的分数
        for (int i = 0; i < 10; ++i) {
            tempScores.push_back(rand() % 100);  
        }
        
        // 排序并去掉最高最低分
//        std::sort(tempScores.begin(), tempScores.end());
//        tempScores.pop_back();
//        tempScores.pop_front();
        
        // 将处理后的分数存入选手对象
        for (int score : tempScores) {
            player.add_score(score);
        }
    }

    // 输出结果
    for (auto& player : players) {
        std::cout << player.cal_ave_score() << std::endl;
    }
}

int main()
{
    test();
    return 0;
}

